package com.naiterui.ehp.bs.cms.modules.base.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.base.service.IHospitalService;
import com.naiterui.ehp.bs.cms.modules.base.vo.HospitalPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * @author gaoliangliang
 * @date 2021/2/24 下午3:32
 **/
@Slf4j
@RequiredArgsConstructor
@RestController
@Api(tags = {SwaggerTag.CMS + SwaggerTag.BASIC_INFO})
@RequestMapping("/base/hospital")
@PreAuthorize("@p.check('base:hospital')")
public class HospitalController {

    private final IHospitalService hospitalService;

    @SysLog("医院列表")
    @ApiOperation("医院列表")
    @GetMapping("/list")
    // @PreAuthorize("@p.check('base:hospital:list')")
    public PageVO<HospitalPageVO> list(PageParamsVO pageParamsVO, HospitalPageVO hospitalPageVO) {
        return hospitalService.queryPage(pageParamsVO, hospitalPageVO);
    }

    @SysLog("新增/编辑医院")
    @ApiOperation("新增/编辑医院")
    @PostMapping("/save")
    @Validated
    // @PreAuthorize("@p.check('base:hospital:save')")
    public void save(@RequestBody HospitalPageVO hospitalPageVO) throws BusinessException {
        hospitalService.saveOrUpdate(hospitalPageVO);
    }

    @SysLog("新增/编辑医院")
    @ApiOperation("新增/编辑医院")
    @PutMapping("{hospitalId}")
    @ApiImplicitParam(name = "hospitalId", value = "医院ID", dataType = "long", paramType = "path", required = true)
    @Validated
    // @PreAuthorize("@p.check('base:hospital:update')")
    public void update(@PathVariable Long hospitalId, @RequestBody HospitalPageVO hospitalPageVO)
        throws BusinessException {
        hospitalPageVO.setId(hospitalId);
        hospitalService.saveOrUpdate(hospitalPageVO);
    }

    @SysLog("修改医院状态")
    @ApiOperation("修改医院状态")
    @PostMapping("/hospitalStatus/{hospitalId}/{status}")
    // @PreAuthorize("@p.check('base:hospital:status')")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "hospitalId", value = "医院ID", dataType = "long", paramType = "path", required = true),
        @ApiImplicitParam(name = "status", value = "状态 0:正常，1:禁用", dataType = "String", paramType = "path",
            required = true)})
    public void hospitalStatus(@PathVariable("hospitalId") Long hospitalId,
        @PathVariable("status") @Max(value = 1, message = "状态不正确") @Min(value = 0, message = "状态不正确") Integer status) {
        hospitalService.updateHospitalStatus(hospitalId, status);
    }

    @SysLog("医院详情")
    @GetMapping("/detail/{hospitalId}")
    @ApiOperation("医院详情")
    @ApiImplicitParam(name = "hospitalId", value = "医院ID", dataType = "long", paramType = "path", required = true)
    // @PreAuthorize("@p.check('base:hospital:detail')")
    public HospitalPageVO details(@PathVariable("hospitalId") Long hospitalId) {
        return hospitalService.queryByHospitalId(hospitalId);
    }
}
